package lk.icta.common.web;

import lk.icta.common.AppConstants;
import lk.icta.core.dao.AlienationDAO;
import lk.icta.core.dto.AlienationDetails;
import lk.icta.core.entity.Alienation;
import lk.icta.core.service.AlienationManagementService;
import lk.icta.core.service.LandApplicationManagementService;
import lk.icta.user.common.Constant;
import lk.icta.user.entity.User;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Duminda Dharmakeerthi
 */
public class JSONAlienationHistoryLookupService extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(JSONAlienationHistoryLookupService.class);
    private final ObjectMapper mapper = new ObjectMapper();
    private AlienationDAO alienationDAO;
    private AlienationManagementService alienationManagementService;


    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
        alienationDAO = (AlienationDAO) context.getBean("alienationDAO");
        alienationManagementService = (AlienationManagementService) context.getBean("alienationManagementService");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String grantId = request.getParameter(WebConstants.GRANT_ID);
        HashMap<String, Object> optionLists;
        try {
            logger.debug("Alienation History lookup");
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(AppConstants.SESSION_USER_BEAN);
            optionLists = new HashMap<String, Object>();
            List<AlienationDetails> alienationList = new ArrayList<AlienationDetails>();
            //  alienationList = alienationDAO.getGrantAlienationHistory(Integer.parseInt(grantId), user);
            // alienationList.add(alienationDAO.getAlienationById(Long.parseLong(grantId), user));
            alienationList = alienationManagementService.getAlienationHistoryByGrantId(Integer.parseInt(grantId), user);
            optionLists.put("alienationList", alienationList);
            logger.debug("Listing Alienation related to grant {} are {}", grantId, alienationList.size());
        } catch (Exception e) {
            logger.error("Fatal Error : {}", e);
            return;
        }

        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        mapper.writeValue(out, optionLists);
        out.flush();
    }
}
